/* %----------------------------------------------------------------------%
   %                     Riadiace okno pre funkciu sinus                  %
   %----------------------------------------------------------------------% */

package riadenie;

import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import platno.*;
import utilities.*;

public class RiadiaciDialog extends Dialog {

	  int defaultB = 1, pripocetInt; 
	  int minSB = 1, maxSB = 1000, pocHod = 500;
	  double pripocet;
	  double defaultA = 1.0, defaultC = 1.0, defaultD = 0.0;
	  boolean bstlacSB = true;
	  Scrollbar aposunSB, bposunSB, cposunSB, dposunSB;
	  TextField avstupTF, bvstupTF, cvstupTF, dvstupTF;
	  Label avystupLB, bvystupLB, cvystupLB, dvystupLB;
	  String s1 = "a = ", s2 = "b = ", s3 = "c = ", s4 = "d = ";
	  String aretazec, aretazec1, cretazec, cretazec1, dretazec, dretazec1, sInt;
	  GridBagConstraints gbc;
	  GridBagLayout gbl;
	  Panel panelLeft, panelA, panelB, panelC, panelD;
	  Panel panelCenter, panelE, panelF, panelG, panelH;
	  Panel panelRight, panelI, panelJ, panelK, panelL;
	  Orezanie or;
	  Kresli kr;
	  Parametre p = null;

      public RiadiaciDialog(Frame vlastnik, boolean stav) {

	     super(vlastnik, "MojDialog", stav);
	     this.setLayout(new GridLayout(1, 3));
//	     p = new Parametre();
	     or = new Orezanie();

//----------------------------------------------------------------
	     panelLeft = new Panel();
	     panelLeft.setLayout(new GridLayout(4, 1));
	     panelLeft.setBackground(new Color(0, 100, 100));

	     avystupLB = new Label(s1 + String.valueOf(defaultA) + "            ");
	     bvystupLB = new Label(s2 + String.valueOf(defaultB) + "            ");
	     cvystupLB = new Label(s3 + String.valueOf(defaultC) + "            ");
	     dvystupLB = new Label(s4 + String.valueOf(defaultD) + "            ");	     
	     
	     panelA = new Panel();
	     panelA.setLayout(new FlowLayout(FlowLayout.LEFT));
	     panelA.setBackground(new Color(200, 20, 20));
	     panelA.add(avystupLB);

	     panelB = new Panel();
	     panelB.setLayout(new FlowLayout(FlowLayout.LEFT));
	     panelB.add(bvystupLB);

	     panelC = new Panel();
	     panelC.setLayout(new FlowLayout(FlowLayout.LEFT));
	     panelC.add(cvystupLB);

	     panelD = new Panel();
	     panelD.setLayout(new FlowLayout(FlowLayout.LEFT));
	     panelD.add(dvystupLB);

	     panelLeft.add(panelA);
	     panelLeft.add(panelB);
	     panelLeft.add(panelC);
	     panelLeft.add(panelD);

//----------------------------------------------------------------

	     panelCenter = new Panel();
	     panelCenter.setLayout(new GridLayout(4, 1));

	     avstupTF = new TextField(String.valueOf(defaultA), 7);
	     bvstupTF = new TextField(String.valueOf(defaultB), 7);
	     cvstupTF = new TextField(String.valueOf(defaultC), 7);
	     dvstupTF = new TextField(String.valueOf(defaultD), 7);

	     panelE = new Panel();
	     panelE.setLayout(new FlowLayout(FlowLayout.LEFT));
	     panelE.add(avstupTF);

	     panelF = new Panel();
	     panelF.setLayout(new FlowLayout(FlowLayout.LEFT));
	     panelF.add(bvstupTF);

	     panelG = new Panel();
	     panelG.setLayout(new FlowLayout(FlowLayout.LEFT));
	     panelG.add(cvstupTF);

	     panelH = new Panel();
	     panelH.setLayout(new FlowLayout(FlowLayout.LEFT));
	     panelH.add(dvstupTF);

	     panelCenter.add(panelE);
	     panelCenter.add(panelF);
	     panelCenter.add(panelG);
	     panelCenter.add(panelH);

//----------------------------------------------------------------

	     gbl = new GridBagLayout();
	     gbc = new GridBagConstraints();

	     panelRight = new Panel();
	     panelRight.setLayout(new GridLayout(4, 1));

	     aposunSB = new Scrollbar(Scrollbar.HORIZONTAL, pocHod, 0, minSB, maxSB);
	     bposunSB = new Scrollbar(Scrollbar.HORIZONTAL, pocHod, 0, minSB, maxSB);
	     cposunSB = new Scrollbar(Scrollbar.HORIZONTAL, pocHod, 0, minSB, maxSB);
	     dposunSB = new Scrollbar(Scrollbar.HORIZONTAL, pocHod, 0, minSB, maxSB);

	     panelI = new Panel();
	     panelI.setLayout(gbl);
	     gbc.weightx = 1.0;
	     gbc.weighty = 0.0;
	     gbc.ipadx = 30;
	     gbl.setConstraints(aposunSB, gbc);
	     panelI.add(aposunSB);

	     panelJ = new Panel();
	     panelJ.setLayout(gbl);
	     gbl.setConstraints(bposunSB, gbc);
	     panelJ.add(bposunSB);

	     panelK = new Panel();
	     panelK.setLayout(gbl);
	     gbl.setConstraints(cposunSB, gbc);
	     panelK.add(cposunSB);

	     panelL = new Panel();
	     panelL.setLayout(gbl);
	     gbl.setConstraints(dposunSB, gbc);
	     panelL.add(dposunSB);

	     panelRight.add(panelI);
	     panelRight.add(panelJ);
	     panelRight.add(panelK);
	     panelRight.add(panelL);

//----------------------------------------------------------------

	     this.add(panelLeft);
	     this.add(panelCenter);
	     this.add(panelRight);

	     this.pack();

	     avstupTF.addActionListener(new ActionListener() {
		        public void actionPerformed(ActionEvent e) {
		        	
                   aposunSB.setValue(500);
		           avystupLB.setText(s1.concat(avstupTF.getText())); 
		           
		           p.setA(Double.valueOf(avstupTF.getText()).doubleValue());  
//		           avystupLB.setText(s1.concat(String.valueOf(p.getA())));
		           kr.maluj();            
		 
		        } 
		 });
	     bvstupTF.addActionListener(new ActionListener() {
		        public void actionPerformed(ActionEvent e) {
		        	
		           sInt = or.zistiIntAleboDouble(bvstupTF.getText());
		           bvstupTF.setText(sInt);
                   bposunSB.setValue(500);
                   bvystupLB.setText(s2.concat(sInt));
                   
                   p.setB(Integer.valueOf(sInt).intValue());
                   bstlacSB = true;
		           kr.maluj();

		        } 
		 });
	     cvstupTF.addActionListener(new ActionListener() {
		        public void actionPerformed(ActionEvent e) {

                   cposunSB.setValue(500);
                   cvystupLB.setText(s3.concat(cvstupTF.getText()));
                   
		           p.setC(Double.valueOf(cvstupTF.getText()).doubleValue()); 
		           kr.maluj();
		 
		        } 
	     });
	     dvstupTF.addActionListener(new ActionListener() {
		        public void actionPerformed(ActionEvent e) {

                   dposunSB.setValue(500);
                   dvystupLB.setText(s4.concat(dvstupTF.getText()));
                
		           p.setD(Double.valueOf(dvstupTF.getText()).doubleValue());  
		           kr.maluj();
		 
		        } 
	     });
	     aposunSB.addAdjustmentListener(new AdjustmentListener() {
		        public void adjustmentValueChanged(AdjustmentEvent e) {

		           pripocet = ((double)(aposunSB.getValue() - 500)) * 0.1;
		           aretazec = String.valueOf((Double.valueOf(avstupTF.getText()).doubleValue() + pripocet));
		           aretazec1 = avstupTF.getText();
	               avystupLB.setText(s1.concat(or.orez(aretazec, aretazec1)));

	               p.setA(Double.valueOf(or.orez(aretazec, aretazec1)).doubleValue());            
	               kr.maluj();
		 
		        } 
		 });
	     bposunSB.addAdjustmentListener(new AdjustmentListener() {
		        public void adjustmentValueChanged(AdjustmentEvent e) {

    	           sInt = or.zistiIntAleboDouble(bvstupTF.getText());
		           bvstupTF.setText(sInt);
		           if(bstlacSB){		        	   
	                   bposunSB.setValue(500);		        	   
		           }
		           bstlacSB = false;
		           pripocetInt = bposunSB.getValue() - 500;
		           bvystupLB.setText(s2.concat(String.valueOf((Integer.valueOf(sInt).intValue() + pripocetInt))));   
		           
		           p.setB(Integer.valueOf(sInt).intValue() + pripocetInt);   
		           kr.maluj();
		        } 
	     });
	     cposunSB.addAdjustmentListener(new AdjustmentListener() {
		        public void adjustmentValueChanged(AdjustmentEvent e) {

		           pripocet = ((double)(cposunSB.getValue() - 500)) * 0.1;
		           cretazec = String.valueOf((Double.valueOf(cvstupTF.getText()).doubleValue() + pripocet));
		           cretazec1 = cvstupTF.getText();
		           cvystupLB.setText(s3.concat(or.orez(cretazec, cretazec1)));

		           p.setC(Double.valueOf(or.orez(cretazec, cretazec1)).doubleValue());            
		           kr.maluj();
//		           par.setC(Double.valueOf(cvstupTF.getText()).doubleValue());            
		 
		        } 
	     });
	     dposunSB.addAdjustmentListener(new AdjustmentListener() {
		        public void adjustmentValueChanged(AdjustmentEvent e) {

		           pripocet = ((double)(dposunSB.getValue() - 500)) * 0.1;
		           dretazec = String.valueOf((Double.valueOf(dvstupTF.getText()).doubleValue() + pripocet));
		           dretazec1 = dvstupTF.getText();
		           dvystupLB.setText(s4.concat(or.orez(dretazec, dretazec1)));

		           p.setD(Double.valueOf(or.orez(dretazec, dretazec1)).doubleValue());            
		           kr.maluj();
//		           par.setD(Double.valueOf(dvstupTF.getText()).doubleValue());            
		 
		        } 
	     });
	     addWindowListener(new WindowAdapter() {
	       public void windowClosing(WindowEvent e) {	       
	          System.exit(1);  
	       }
	     });

	     this.setSize(300, 300);
	     this.setLocation(700, 100);
	     this.setVisible(true);

      }
      public void setKrRef(Kresli kr){ 
    	  this.kr = kr;                      	  
      }
      public void setPRef(Parametre p){    	  
    	  this.p = p;    	  
      }
}